package ge.modules.drawables;


import ge.engine.Engine;
import ge.exceptions.DrawEngineException;

 



/**
 * 	
 * @author Abel
 *	Represents each animation of the sprite
 */
public class Animation
{
	/**
	 * If the animation has to start again when it finishes
	 */
	private boolean looped;
	/**
	 * The lists of frames who compose the animation
	 */
	
	private int[] list;
	
	/**
	 * 	Current frame to be drawn
	 */
	private Integer currentFrame;
	private float time;
	private float fps;
	/**
	 * 	Constructor defines the default features
	 * @param list	List of frames in the animation
	 * @param fps	The velocity of the animation (frames per second)
	 * 
	 */
	public Animation(int[] list, int fps) 
	{
		try {
			
			if(list.length == 0)
				throw new DrawEngineException("Animation list is empty");
			this.list = list;
			looped = false;
			currentFrame = 0;
			time = 0;
			this.fps = fps;
		} catch (DrawEngineException e) {
			 
			e.printStackTrace();
		}
	}
	
	/**
	 * 		Sets loop feature
	 * @param loop True to enable
	 */
	public void setLoop(boolean loop)
	{
		this.looped = loop;
	}
	/**
	 * 	@return looped feature
	 */
	public boolean getLoop()
	{
		return looped;
	}
	/**
	 * 	Returns currentFrame
	 * @return current frame
	 */
	public Integer getCurrentFrame()
	{
		return list[currentFrame];
	}
	/**
	 * 	Returns current frame and move the animation to the next one.
	 * @return Current Frame
	 */
	public Integer nextFrame()
	{
		time+= Engine.tick();
 
		if(time >= 1000/fps)
		{
			time = 0;
			if(currentFrame == list.length-1 )
			{
				if(looped)
				{
					currentFrame = 0;
				}
			}
			else
			{
				currentFrame++;
				
			}
			 
		}
	 
			
		
		
		return  getCurrentFrame();
	}
	/**
	 * Sets the current frame of the animation to the begining
	 */
	public void init()
	{
		currentFrame = 0;
	}
	/**
	 * 
	 * @return Return true if the animation current frame is the last one
	 */
	public boolean isFinish()
	{
		return currentFrame == list.length-1;
	}
	

}